package product;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.DecimalFormat;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DelList extends JFrame {
	Connection con;
	CodeList cl;
	DefaultTableModel model;
	Vector<String> col = new Vector<String>();
	DecimalFormat df = new DecimalFormat("###,###,###.##");
	
	private JPanel panel;
	private JTextField textField;
	private JButton button;
	private JScrollPane scrollPane;
	private JTable table;
	private JPanel panel_1;
	private JButton btnNewButton;
	private JTextField textField_1;

	/**
	 * Create the frame.
	 */
	public DelList(Connection c, CodeList cl) {
		setTitle("삭제 코드 리스트");
		this.con = c;
		this.cl = cl;
		setBounds(100, 100, 480, 346);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		getContentPane().add(getPanel_1(), BorderLayout.SOUTH);
		
		col.add("제품 코드");
		col.add("제품 이름");
		col.add("제품 원가");
		col.add("등록자");
		col.add("등록일");
		col.add("수정자");
		col.add("수정일");
		col.add("거래처");
		model = new DefaultTableModel(col, 0) {
			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}
		};
		
		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(false);
	}
	
	public void search() { // 제품 코드에 '#'이 있는 코드 검색
		model.setRowCount(0);
		String find = textField.getText();

		String sql = "SELECT SUBSTR(C.PRO_CODE, 2, 5), C.PRO_NM, PRO_PRICE, M.IRUM, PRO_REG_DATE, M.IRUM, PRO_MOD_DATE, PRO_CS_NO"
				+ " FROM MEMBERDATA M INNER JOIN CODE C"
				+ " ON M.MEMBERID = C.PRO_REG_MEM"
				+ " WHERE C.PRO_CODE LIKE ? AND (C.PRO_CODE LIKE ? OR C.PRO_NM LIKE ? OR M.IRUM LIKE ?)"
				+ " ORDER BY PRO_CODE";
		try {
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "#%");
			pstmt.setString(2, "%"+find+"%");
			pstmt.setString(3, "%"+find+"%");
			pstmt.setString(4, "%"+find+"%");
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();

			while (rs.next()) {
				Vector<String> row = new Vector<String>();
				row.add(rs.getString(1)); // 제품 코드
				row.add(rs.getString(2)); // 제품 이름
				row.add(df.format(Integer.parseInt(rs.getString(3))) + "원"); // 단가
				row.add(rs.getString(4)); // 등록자
				row.add(rs.getString(5)); // 등록일
				row.add(rs.getString(6)); // 수정자
				row.add(rs.getString(7)); // 수정일
				row.add(rs.getString(8)); // 거래처
				model.addRow(row);
			}
			table.setModel(model);
			
			DefaultTableCellRenderer Center = new DefaultTableCellRenderer();
			Center.setHorizontalAlignment(SwingConstants.CENTER); // 가운데 정렬
			TableColumnModel tcmSchedule = table.getColumnModel();
			DefaultTableCellRenderer celAlignRight = new DefaultTableCellRenderer();
			celAlignRight.setHorizontalAlignment(JLabel.RIGHT);

			tcmSchedule.getColumn(0).setCellRenderer(Center);
			tcmSchedule.getColumn(1).setCellRenderer(Center);
			tcmSchedule.getColumn(2).setCellRenderer(celAlignRight);
			tcmSchedule.getColumn(3).setCellRenderer(Center);
			tcmSchedule.getColumn(4).setCellRenderer(Center);
			tcmSchedule.getColumn(5).setCellRenderer(Center);
			tcmSchedule.getColumn(6).setCellRenderer(Center);
			tcmSchedule.getColumn(7).setCellRenderer(Center);
			
			rs.close();
			pstmt.close();
		} catch (Exception e) {
		}

	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getTextField(), BorderLayout.CENTER);
			panel.add(getButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						search();
					}
				}
			});
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("     조회     ");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					search();
				}
			});
		}
		return button;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					int row = table.getSelectedRow();
					textField_1.setText(("" + table.getValueAt(row, 0)));
				}
			});
		}
		return table;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.add(getTextField_1());
			panel_1.add(getBtnNewButton());
		}
		return panel_1;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("코드 복원");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String code = textField_1.getText();
					try {
						String sql = "UPDATE CODE SET PRO_CODE = SUBSTR(PRO_CODE,2,5), PRO_MOD_DATE = SYSDATE WHERE PRO_CODE = '#'||?";
						PreparedStatement pstmt = con.prepareStatement(sql);
						pstmt.setString(1, code);
						pstmt.executeUpdate();
						JOptionPane.showMessageDialog(null, "코드가 복원되었습니다.");
						search();
						cl.textField.setText(code);
						cl.start();
						pstmt.close();
					} catch (Exception e2) {
						// TODO: handle exception
					}
					
					
				}
			});
		}
		return btnNewButton;
	}
	private JTextField getTextField_1() {
		if (textField_1 == null) {
			textField_1 = new JTextField();
			textField_1.setColumns(10);
		}
		return textField_1;
	}
}
